#include "dominion.h"
#include "dominion_helpers.h"
#include "rngs.h"
#include <stdio.h>
#include <math.h>
#include <stdlib.h>
#include "testutilities.h"
#include <assert.h>




int testUpdateCoins(struct gameState *g){
	int i;
	int coins = 0;
	
	for(i=0; i< g->handCount[g->whoseTurn]; i++){
		if(g->hand[g->whoseTurn][i] == copper){
			coins ++;
		}
		if(g->hand[g->whoseTurn][i] == silver){
			coins = coins + 2;
		}
		if(g->hand[g->whoseTurn][i] == gold){
			coins = coins + 3;
		}
	}
	
//	printf("coins: %d")
	updateCoins(g->whoseTurn, g, 0);
//	printf("coins: %d")
	assert (coins == g->coins);
	g->bonus ++;
	updateCoins(g->whoseTurn, g, 0);
	assert(coins+1 == g->coins);
	g->bonus ++;
	updateCoins(g->whoseTurn, g, 0);
	assert(coins+2 == g->coins);


	return 1;
}

void testIntegration(struct gameState *g){


	g->numPlayers = 2;
	g->whoseTurn = 1;
	g->numActions = 10;
	g->hand[g->whoseTurn-1][1] = copper;
	g->hand[g->whoseTurn-1][0] = silver;
	g->handCount[g->whoseTurn-1] = 2;
	g->hand[g->whoseTurn][8] = minion;
	g->hand[g->whoseTurn][7] = cutpurse;
	g->hand[g->whoseTurn][6] = embargo;
	g->hand[g->whoseTurn][5] = steward;
	g->hand[g->whoseTurn][4] = salvager;
	g->hand[g->whoseTurn][3] = estate;
	g->hand[g->whoseTurn][2] = copper;
	g->hand[g->whoseTurn][1] = silver;
	g->hand[g->whoseTurn][0] = tribute;
	updateCoins(g->whoseTurn, g, 0);
	int expected = g->coins;
	printf("Before: %d\n", expected);
	
	playCard(8, 1, -1, -1, g);
	expected += 2;
	printf("After Minion: Expected: %d Actual: %d\n", expected, g->coins);
//	assert(g->coins == (expected));
	expected = g->coins;
	playCard(7, -1, -1, -1, g);
	expected += 2;
	printf("After cutpurse: Expected: %d Actual: %d\n", expected, g->coins);
//	assert(g->coins == (expected));
	expected = g->coins;
	playCard(6, 3, -1, -1, g);
	expected += 2;
	printf("After embargo: Expected: %d Actual: %d\n", expected, g->coins);
//	assert(g->coins == (expected));

	expected = g->coins;
	playCard(5, 2, -1, -1, g);
	expected += 2;
	printf("After steward: Expected: %d Actual: %d\n", expected, g->coins);
//	assert(g->coins == (coinsBefore +8));

	expected = g->coins;
	playCard(4, 3, -1, -1, g);
	expected += 2;
	printf("After salvager - estate: Expected: %d Actual: %d\n", expected, g->coins);
//	assert(g->coins == (coinsBefore +9));

	expected = g->coins;
	playCard(0, -1, -1, -1, g);
	expected += 4;
	printf("After tribute: Expected: %d Actual: %d\n", expected, g->coins);




}

int main(int argc, char** argv){
	int tests = 1000;
	int seed = 1;

	if(argc > 1){
		tests = atoi(argv[1]);
	}
	if(argc > 2){
		seed = atoi(argv[2]);
	}
	prepRandom(1, seed);
	printf("Testing updateCoins() tests: %d seed: %d\n", tests, seed);
	int i;
	for(i=0; i<tests; i++){
		struct gameState *g = newGame();
		makeRandomGameState(g);
		g->bonus = 0;
		testUpdateCoins(g);
		free(g);
	}

	struct gameState *g = newGame();
	
	testIntegration(g);
	printf("Test Complete\n");

return 0;
}
